package com.yixin.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.entity.domain.Dept;
import com.yixin.hospital.entity.domain.WorkAddress;
import com.yixin.hospital.entity.dto.req.workaddress.WorkAddressAddReqDTO;
import com.yixin.hospital.entity.dto.req.workaddress.WorkAddressEditReqDTO;
import com.yixin.hospital.entity.vo.WorkAddressVO;
import com.yixin.hospital.mapper.WorkAddressMapper;
import com.yixin.hospital.service.DeptService;
import com.yixin.hospital.service.WorkAddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author wb-caifenghua
* @description 针对表【work_address(工作地点表)】的数据库操作Service实现
* @createDate 2024-03-22 14:35:04
*/
@Service
public class WorkAddressServiceImpl extends ServiceImpl<WorkAddressMapper, WorkAddress>
    implements WorkAddressService{

    @Resource
    private DeptService deptService;


    @Override
    public ApiResult allList() {
        List<WorkAddress> list = this.list();
        List<WorkAddressVO> workAddressVOS = BeanUtil.copyToList(list, WorkAddressVO.class);
        for (WorkAddressVO workAddressVO : workAddressVOS) {
            Dept dept = deptService.getById(workAddressVO.getDeptId());
            workAddressVO.setDeptName(dept.getName());
        }
        return ApiResult.success(workAddressVOS);
    }

    @Override
    public ApiResult add(WorkAddressAddReqDTO workAddressAddReqDTO, String userName) {
        WorkAddress workAddress = new WorkAddress();
        workAddress.setName(workAddressAddReqDTO.getName());
        workAddress.setDeptId(workAddressAddReqDTO.getDeptId());
        workAddress.setCreatedBy(userName);
        workAddress.setUpdatedBy(userName);
        this.save(workAddress);
        return ApiResult.success();
    }

    @Override
    public ApiResult edit(WorkAddressEditReqDTO workAddressEditReqDTO, String userName) {
        WorkAddress workAddress = new WorkAddress();
        workAddress.setId(workAddressEditReqDTO.getId());
        workAddress.setName(workAddressEditReqDTO.getName());
        workAddress.setDeptId(workAddressEditReqDTO.getDeptId());
        workAddress.setUpdatedBy(userName);
        this.updateById(workAddress);
        return ApiResult.success();
    }
}




